﻿// JavaScript Document
// itea 2009-07-22

// NBox
var NBox = function(id) {
	if(this == window)
		return NBox.getNBox(id);
};
NBox.nboxMap = {};
NBox.getNBox = function(id) {
	return NBox.nboxMap[id] || null;
};
NBox.setNBox = function(id, nbox) {
	if(nbox)
		NBox.nboxMap[id] = nbox;
	else {
		NBox.nboxMap[id] = null;
		delete NBox.nboxMap[id];
	}
	return nbox || null;
};
NBox.localeSeq = 100;
NBox.getLocaleSeq = function() {
	return NBox.localeSeq++;
};

// NBulletin, present a bulletin box on page
var NBulletin = function(misc) {
	if(this == window) {
		return NBox.getNBox('NBulletin_'+ misc);
	} else {
		this.bulletin = misc;
		this.id = this.bulletin.id;
		this.tagId = 'bulletin_'+ this.bulletin.id;
		this._tagId = '#bulletin_'+ this.bulletin.id;
		NBox.setNBox('NBulletin_'+ this.id, this);
	}
};
$.extend(NBulletin.prototype, {
	genHtml: function() {
		return '<div class="nbox bulletin-view ui-state-highlight ui-corner-all" id="<@id>"></div>'.replace(/<@id>/,this.tagId);
	},
	render: function() {
		$(this._tagId).html(this.bulletin.content);
		return this;
	},
	refresh: function() {
	},
	close: function() {
		$(this._tagId).remove();
		NBox.setNBox('NBulletin_'+ this.id);
	},
	isExpired: function() {
		return this.bulletin.expireTime.getTime() < +new Date() ? true : false;
	}
});

// NNews, present a news box on page
var NNews = function(misc) {
	if(this == window) {
		return NBox.getNBox('NNews_'+ misc);
	} else {
		this.news = misc;
		this.id = this.news.id;
		this.tagId = 'news_'+ this.news.id;
		this._tagId = '#news_'+ this.news.id;
		this.cbox = new CBox(this.news.id);
		NBox.setNBox('NNews_'+ this.id, this);
	}
};

NNews._onNewsClick = function(event) {
	// 'this' here is not NNews instance
	$(this).css('background-color', '#FFFFFB');
	var nnews = NNews(event.data.id);
	var tdiff = nnews.news.expireTime.getTime() - new Date().getTime();
	if(tdiff > 1000*page.clientConfig.readNewsLeftTime) {
		nnews.news.expireTime.setTime(new Date().getTime() + 1000*page.clientConfig.readNewsLeftTime);
	}
};

$.extend(NNews.prototype, {
	genHtml: function() {
		return '<div class="nbox news-view ui-widget-content ui-corner-all" id="<@id>"></div>'.replace(/<@id>/,this.tagId);
	},
	render: function() {
		var ne = this.news;
		var html = ['<input class="top-func" type="button" value="关闭" onclick="NBoard.closeNBox(', this.id,
			')"/><h2 class="time">', this.tstr(ne), '</h2>'];
		if(ne.subject) {
			var s = ne.srcLinkUrl ? ['<a target="_blank" href="read.jsp?id=', this.news.id, '&src=', encodeURI(this.news.srcLinkUrl), '">', ne.subject, '</a>'].join('')
					: ne.subject;
			html.push('<h1 class="subject">', s, '</h1>');
		}
		if(ne.imageUrl)
			html.push('<img src="', encodeURI(ne.imageUrl), '"/>');
		if(ne.content)
			html.push(ne.content);
		html.push(this.cbox.genHtml());
		html.push('<form onsubmit="NNews(<@id>).postComment();return false;">评论：<input id="cinput_<@id>" class="comment-input" type="text" /> <input type="submit" value="发表"/></form>'
			.replace(/<@id>/g, this.id));
		
		html.push('<div class="bottom-info clear"><ul><li>已读人数: ', ne.readCount, '</li>');
		if(ne.srcLinkUrl)
			html.push('<li><a target="_blank" href="', encodeURI(ne.srcLinkUrl),'">来源</a></li>');
		if(ne.poster) {
			html.push('<li>投递: <@poster></li>'
				.replace(/<@poster>/, ne.posterLink ? ['<a href="', encodeURI(ne.posterLink), '">', ne.poster, '</a>'].join('') : ne.poster));
		}
		html.push('</ul><div class="clear"></div></div>');
		$(this._tagId).append(html.join(''))
			.effect('highlight', {}, 'slow')
			.one('click', {id: this.id}, NNews._onNewsClick);
		return this;
	},
	tstr: function(ne) {
		var p = ne.publishTime;
		var t = ne.expireTime;
		var currt = new Date();
		var s = '';
		if(p.toLocaleDateString() != new Date().toLocaleDateString())
			s += p.getDate() +'日 ';
		var leftt = new Date();
		leftt.setTime(t.getTime() - currt.getTime() + currt.getTimezoneOffset()*60*1000);
		var left = leftt.toLocaleTimeString();
		return s + p.toLocaleTimeString() +' ('+left+')';
		//t.getHours() +":"+ t.getMinutes() +":"+ t.getSeconds();
	},
	postComment: function() {
		var c = $.trim($('#cinput_'+ this.id).val());
		if(c) {
			if(c.length > 140) {
				alert('评论太长啦，长话短说吧');
				return false;
			}
			this.cbox.add(c);
			$('#cinput_'+ this.id).val('');
		}
		return false;
	},
	refresh: function() {
		$(this._tagId+' h2.time').text(this.tstr(this.news));
		this.cbox.refresh();
	},
	close: function() {
		$(this._tagId).remove();
		NBox.setNBox('NNews_'+ this.id);
	},
	isExpired: function() {
		return this.news.expireTime.getTime() < +new Date() ? true : false;
	}
});

// Comment box
var CBox = function(misc) {
	if(this == window) {
		return NBox.getNBox('CBox_'+ misc);
	} else {
		this.newsId = this.id = misc;
		this.tagId = 'cmts_'+ this.newsId;
		this._tagId = '#cmts_'+ this.newsId;
		this.cMap = {}; // key: Comment.content, value: Comment
		this.cIdx = {}; // comment Index. key: Comment.id, value: Comment
		this.raisingComments = []; // 记录自从上次评论更新后的顶过的评论id
		this.newComments = []; // 记录自从上次评论更新后用户增加的新评论
		NBox.setNBox('CBox_'+ this.id, this);
	}
};

$.extend(CBox.prototype, {
	genHtml: function() {
		return '<div id="<@tagId>" class="comments"><div class="clear"></div></div>'.replace('<@tagId>',this.tagId);
	},
	render: function(comments) {
		comments == comments || [];
		var html = [];
		var cmap = {};
		var cidx = {};
		for(var i=0, j=comments.length; i<j; i++) {
			var c = comments[i];
			cmap[c.content] = c;
			cidx[c.id] = c;
			html.push(this.genCommentHtml(c));
		}
		
		this.cMap = cmap;
		this.cIdx = cidx;
		$(this._tagId).children('.comment').remove().end().prepend(html.join(''));
		$(this._tagId +' .comment').effect('highlight', {}, 'slow');
		return this;
	},
	refresh: function() {
		var that = this;
		newsDwr.getComments(this.newsId, this.newComments, this.raisingComments, function(comments){
			that.render(comments);
			that.newComments = [];
			that.raisingComments = [];
		});
		return this;
	},
	close: function() {
		$(this._tagId).remove();
		NBox.setNBox('CBox_'+ this.id);
	},
	isExpired: function() {
		return false;
	},
	add: function(comment) {
		var c = this.cMap[comment];
		if(c) {
			if($.cookie('cmt_'+ c.id) != 'Y')
				this.raiseComment(c.id);
		} else {
			c = {newsId: this.newsId, content: comment, raiseCount: 0, id: NBox.getLocaleSeq(), createTime: new Date()};
			newsDwr.postComment(c);
			//this.newComments.push(c);
			this.cMap[comment] = c;
			this.cIdx[c.id] = c;
			var htmls = ['<span id="cmt_', c.id, '" class="comment ui-state-active ui-corner-all fs0">', c.content, '</span>'];
			$(this._tagId).prepend(htmls.join(''));
		}
		$('#cmt_'+ c.id).effect('highlight', {}, 'slow');
		return this;
	},
	genCommentHtml: function(c) {
		var d = ($.cookie('cmt_'+ c.id) == 'Y')
			? (c.raiseCount)
			: ['<a href="javascript:CBox(', this.id, ').raiseComment(', c.id, ')">顶</a>'].join('');
		return ['<span id="cmt_', c.id, '" class="comment ui-state-default ui-corner-all ',
			this.getCommentSize(c.raiseCount), '">', c.content.replace(/</g,'&lt;').replace(/>/g,'&gt;'),
			'<span class="comment-up ui-corner-right">', d, '</span></span>'].join('');
	},
	raiseComment: function(commentId) {
		var c = this.cIdx[commentId];
		if(c) {
			$('#cmt_'+ c.id).removeClass('ui-state-default').addClass('ui-state-active')
				.children('.comment-up').text(++c.raiseCount);
			this.raisingComments.push(c.id);
			$.cookie('cmt_'+ c.id, 'Y', {expires: 1});
		}
	},
	// 根据顶的次数返回评论的字体大小的类
	getCommentSize: function(c) {
		if(c < 4)
			return 'fs0';
		else if(c < 7)
			return 'fs1';
		else if(c < 10)
			return 'fs2';
		else if(c < 14)
			return 'fs3';
		else if(c < 19)
			return 'fs4';
		else if(c < 24)
			return 'fs5';
		else if(c < 30)
			return 'fs6';
		else if(c < 38)
			return 'fs7';
		else if(c < 50)
			return 'fs8';
		else
			return 'fs9';
	}
});